import { z } from "zod";
const zodDefaultCatch = <T extends z.ZodType<any, any>>(schema: T, defaultValue: z.infer<T>) => schema.default(defaultValue).catch(defaultValue)

const zodString = (defaultValue = "") => zodDefaultCatch(z.string(), defaultValue);

const zodNumber = (defaultValue = 0) => zodDefaultCatch(z.number(), defaultValue);

const zodArray = <T extends z.ZodType<any, any>>(schema: T, defaultValue: z.infer<T>[] = []) => zodDefaultCatch(z.array(schema), defaultValue);

export const ajaxDefaultSchema = z.object({
  data: z.any(),
  message: zodString(),
  status: zodNumber(),
})

const getWrappedSchema = <T extends z.ZodType<any, any>>(
  schema: T,
  defaultValue: z.infer<T>,
) => ajaxDefaultSchema.extend({
  data: zodDefaultCatch(schema, defaultValue),
})

/**
 * 每日高光地图
 */
export const ajaxMapManageListSchema = getWrappedSchema(zodArray(z.object({
  "imgUrl": zodString(),
  "name": zodString(),
  "author": zodString(),
  "info": zodString(),
  "mapId": zodString(),
})), [])